﻿Module Module1
    Declare Auto Function IsUserAdmin Lib "setupapi.dll" () As Boolean
    Friend requireadmin As Boolean = False

    Public Sub Start()
        Dim names() As String = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").GetValueNames()
        Dim startnames() As String = System.IO.Directory.GetFiles("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup")
        If Not startnames.Length = 0 _
        Or Not My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").GetValue("Startup Manager") = "C:\Program Files\Startup Manager\Startup Manager.exe" _
        Or Not names.Contains("Startup Manager") Then
            requireadmin = True
        End If

        Try
            If My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").ValueCount > My.Computer.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\", True).GetValue("Unmanaged Programs") Then
                requireadmin = True
            End If
        Catch
        End Try
        If startnames.Length + names.Length < Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\").GetValue("Unmanaged Programs") Then
            Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\", True).SetValue("Unmanaged Programs", startnames.Length + Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").ValueCount)
        End If
        If requireadmin = True Then
            If IsUserAdmin = True Then
start:          Try
                    If My.Application.CommandLineArgs.Count >= 1 Then
                        If Not My.Computer.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\").GetValue("Version") = My.Application.CommandLineArgs(0) Then
                            My.Computer.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\", True).SetValue("Version", My.Application.CommandLineArgs(0))
                        End If
                    End If
                    If My.Application.CommandLineArgs Is Nothing Or My.Application.CommandLineArgs.Count = 0 Then
                        My.Computer.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\", True).SetValue("LocationofAAUSP", Reflection.Assembly.GetExecutingAssembly().Location())
                        Exit Sub
                    End If
                    If Not My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup").GetValueNames.Contains("Startup Manager Service") And Not My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").GetValueNames.Contains("Startup Manager Service") Then
                        My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).SetValue("Startup Manager Service", "C:\Program Files\Startup Manager\Startup Manager Service.exe")
                    End If
                Catch ex As Exception
                    If ex.Message = "Object reference not set to an instance of an object." Then
                        My.Computer.Registry.LocalMachine.OpenSubKey("Software\", True).CreateSubKey("Startup Manager")
                        GoTo start
                    Else
                        Try
                            My.Computer.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\", True).SetValue("LocationofAAUSP", Reflection.Assembly.GetExecutingAssembly().Location())
                            Exit Sub
                        Catch ex2 As Exception
                            MsgBox(ex2.Message)
                            Exit Sub
                        End Try
                    End If
                End Try

                If Not names.Contains("Startup Manager") Then ' And Debugger.IsAttached = False Then
                    Try
                        My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).SetValue("Startup Manager", My.Computer.Registry.CurrentUser.OpenSubKey("Software\Startup Manager\").GetValue("Startup Manager Location"))
                        My.Computer.Registry.CurrentUser.OpenSubKey("Software\Startup Manager\", True).DeleteValue("Startup Manager Location")
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                End If
            Else
                If MsgBox("Please elevate to administrator privilages. Click OK to elevate and continue", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                    Dim Proc As New Process
                    Dim ProcStartInfo As New ProcessStartInfo(Reflection.Assembly.GetExecutingAssembly().Location())
                    ProcStartInfo.UseShellExecute = True
                    ProcStartInfo.Verb = "runas"
                    Proc.StartInfo = ProcStartInfo
                    Proc.Start()
                    Throw New Exception("Elevating")
                Else
                    MsgBox("Startup Manager may not work until AAUSP is run with administrator privilages.")
                End If
            End If
        End If
    End Sub

    Public Sub SetVersions()
        Try
            If Not My.Computer.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\").GetValue("Version") = My.Application.CommandLineArgs(0) Then
                My.Computer.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\", True).SetValue("Version", My.Application.CommandLineArgs(0))
                My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).SetValue("Startup Manager", My.Computer.Registry.CurrentUser.OpenSubKey("Software\Startup Manager\").GetValue("Startup Manager Location"))
                My.Computer.Registry.CurrentUser.OpenSubKey("Software\Startup Manager\", True).DeleteValue("Startup Manager Location")
            End If
        Catch
            My.Computer.Registry.LocalMachine.OpenSubKey("Software\Startup Manager\", True).SetValue("Version", My.Application.CommandLineArgs(0))
        End Try
    End Sub

    Public Function GetAllUsersRegistry() As ArrayList
        Dim names() As String = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").GetValueNames()
        Dim itemstomove As New ArrayList

        If My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").ValueCount > 1 Then
            For i As Integer = 0 To My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").ValueCount - 1
                Dim keyvalue As String
                keyvalue = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").GetValue(names(i))
                If Not names(i) = "Startup Manager" Or keyvalue = "Default" Then
                    Dim program As New Program
                    program.Name = names(i)
                    program.Path = keyvalue
                    program.Location = Add_All_Users_Startup_Programs.Program.LocEnum.AllRegistry
                    itemstomove.Add(program)
                    'Status = ("Moving " & names(i))
                    'My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup", True).SetValue(names(i), keyvalue)
                    'My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(names(i))
                End If
            Next
            'My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).CreateSubKey("Managed Startup")
            'GoTo searchreg
        End If

        Return itemstomove
    End Function

    Public Function GetAllUsersStartupFolder() As ArrayList
        Dim startnames() As String = System.IO.Directory.GetFiles("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup")
        Dim itemstomove As New ArrayList

        If Not startnames.Length = 0 Then
            For i As Integer = 0 To startnames.Length - 1
                Dim program As New Program
                program.Name = startnames(i).Substring(61)
                program.Path = startnames(i)
                program.Location = Add_All_Users_Startup_Programs.Program.LocEnum.AllStartMenu
                itemstomove.Add(program)
                'Status = ("Moving " & startnames(i).Substring(61))
                'System.IO.File.Move("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\" & startnames(i).Substring(61), _
                '                    "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\All Managed Startup\" & startnames(i).Substring(61))
            Next
            'System.IO.Directory.CreateDirectory("C:\ProgramData\Microsoft\Windows\Start Menu\Programs\All Managed Startup")
        End If

        Return itemstomove
    End Function

    Public Function GetUserRegistry()
        Dim names() As String = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").GetValueNames()
        Dim itemstoreturn As New ArrayList
        If My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").ValueCount >= 1 Then
searchreg:  Try
                For i As Integer = 0 To My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").ValueCount - 1
                    Dim keyvalue As String
                    keyvalue = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run").GetValue(names(i))
                    If Not names(i) = "Default" Then
                        Dim program As New Program
                        program.Name = names(i)
                        program.Path = keyvalue
                        program.Location = Add_All_Users_Startup_Programs.Program.LocEnum.UserRegistry
                        itemstoreturn.Add(program)
                        'My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run\Managed Startup", True).SetValue(names(i), keyvalue)
                        'My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(names(i))
                    End If
                Next
            Catch
                My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).CreateSubKey("Managed Startup")
                GoTo searchreg
            End Try
        End If
        Return itemstoreturn
    End Function

    Public Function GetUserStartMenu()
        Dim path As String = My.Computer.Registry.CurrentUser.OpenSubKey("Volatile Environment").GetValue("USERPROFILE") & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
        Dim startnames() As String = System.IO.Directory.GetFiles(path)
        Dim name As String = ""
        Dim itemstoreturn As New ArrayList
        If Not startnames.Length = 0 Then
            'movestartmenu: Try
            For i As Integer = 0 To startnames.Length - 1
                Dim program As New Program
                name = startnames(i).Substring(path.Length)
                program.Name = name
                program.Path = path
                program.Location = Add_All_Users_Startup_Programs.Program.LocEnum.UserStartMenu
                itemstoreturn.Add(program)
                'System.IO.File.Move(My.Computer.Registry.CurrentUser.OpenSubKey("Volatile Environment").GetValue("USERPROFILE") & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\" & name, _
                '                    My.Computer.Registry.CurrentUser.OpenSubKey("Volatile Environment").GetValue("USERPROFILE") & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Managed Startup\" & name)
            Next
            'Catch ex As Exception
            'If ex.Message.StartsWith("Cannot create a file") Then
            '    System.IO.File.Delete(My.Computer.Registry.CurrentUser.OpenSubKey("Volatile Environment").GetValue("USERPROFILE") & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Managed Startup\" & name)
            '    GoTo movestartmenu
            'End If
            'System.IO.Directory.CreateDirectory(My.Computer.Registry.CurrentUser.OpenSubKey("Volatile Environment").GetValue("USERPROFILE") & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Managed Startup\")
            'GoTo movestartmenu
            'End Try
        End If
        Return itemstoreturn
    End Function
End Module

Class Program
    Enum LocEnum
        UserRegistry
        AllRegistry
        UserStartMenu
        AllStartMenu
    End Enum

    Private _name As String
    Private _path As String
    Private _Location As LocEnum

    Public Sub New()
    End Sub

    Public Sub New(ByVal Name As String, ByVal Path As String, ByVal Location As LocEnum)
        _name = Name
        _path = Path
        _Location = Location
    End Sub

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property Path() As String
        Get
            Return _path
        End Get
        Set(ByVal value As String)
            _path = value
        End Set
    End Property

    Public Property Location() As LocEnum
        Get
            Return _Location
        End Get
        Set(ByVal value As LocEnum)
            _Location = value
        End Set
    End Property
End Class